WWL.uploader = function ( name )
{
	var uploader = new WWL ( 'uploader', name );

	var orig_name = name;
	var file_id = uploader.id + '-file';
	var filelist_id = uploader.id + '-filelist';

	var row_tpl = '<tr><td class="uploader-filedel"><a href="javascript:WWL.uploader.remove_file(\'' + orig_name + '\',%(index)s)">[del]</a></td><td class="uploader-filename">%(name)s</td><td class="uploader-filesize">%(size)s</td></tr>';

	uploader.type = 0;		// valid: mini, small, full
	uploader.button_class = '';

	uploader.cbacks = {
		"complete" : null,
		"error" : null,
		"cancel" : null
	};

	uploader._is_html5 = liwe.utils.supports ( 'file' );
	uploader._files = [];

	// {{{ HTML5
	uploader._make_html5 = function ()
	{
		var cls = uploader.button_class + ' uploader-button';
		uploader._pb = new WWL.progressbar ( name );
		var s = '<div class="uploader" id="' + uploader.id + '">'
			+ uploader._pb.to_string () 
			+ '<div><input class="uploader-input" id="' + file_id + '" type="file" multiple onchange="WWL.uploader.change(\'' + orig_name + '\',this)" /><div class="' + cls + '">Seleziona files...</div></div>'
			+ '<div class="uploader-filelist"><table id="' + filelist_id + '"></table></div>'
			+ '</div>';

		return s;
	};

	uploader._render_files = function ()
	{
		var list = $ ( filelist_id );
		list.innerHTML = '';
		uploader._files.iterate ( function ( v, k )
		{
			var vals = {
				name: v.name,
				size: uploader._format_size ( v.size ),
				index: k
			};
			list.innerHTML += String.formatDict ( row_tpl, vals );
		} );
	};

	uploader._format_size = function ( s )
	{
		var sizes = [ '', 'k', 'M', 'G', 'T' ];
		var i = 0;
		while ( s >= 1024 )
		{
			s /= 1024;
			i++;
		}
		return ( Math.round ( s * 10 ) / 10 ) + ' ' + sizes [ i ] + 'b';
	};

	uploader._clear_input = function ()
	{
		// hack to clear the input[type=file]
		$( file_id ).parentNode.innerHTML = $( file_id ).parentNode.innerHTML;
	};

	uploader._clear = function ()
	{
		uploader._files = [];
		uploader._render_files ();
	};

	uploader._do_post = function ( form_fields, action, cback )
	{
		if ( uploader._is_html5 )
		{
			uploader._cback = cback;
			var formdata = new FormData ();
			form_fields.iterate ( function ( v, k )
			{
				if ( k == orig_name ) return;
				formdata.append ( k, v );
			} );
			uploader._files.iterate ( function ( file, k )
			{
				formdata.append ( orig_name, file );
			} );

			var req = new XMLHttpRequest ();
			req.open ( 'POST', action, true );
			req.upload.addEventListener ( 'progress', uploader._progress, false );
			req.upload.addEventListener ( 'load', uploader._load, false );
			req.upload.addEventListener ( 'error', uploader._error, false );
			req.upload.addEventListener ( 'abort', uploader._error, false );
			req.send ( formdata );
		}
		else
		{
			uploader.cbacks [ 'complete' ] = cback;

			console.debug ( "CUSTOM SUBMIT: %s", action );
			var p = upl._get_player ();

			form_fields.iterate ( function ( v, k ) { if ( k ) p.setArg ( k, v ); } );
			p.setURL ( action );
			p.send ();
		}
	};

	uploader._progress = function ( e )
	{
		console.debug ( e );
		if ( e.lengthComputable )
			uploader._pb.set_value ( Math.round ( e.loaded / e.total * 100 ) );
	};

	uploader._error = function ( e )
	{
		console.error ( e );
	};

	uploader._load = function ( e )
	{
		uploader._clear ();
		uploader._pb.set_value ( 0 );
		if ( uploader._cback ) uploader._cback ();
	};
	// }}}

	// {{{ FLASH
	uploader._make_flash = function ()
	{
		var name = "swf:" + uploader.id;
		var w, h;

		switch ( uploader.type )
		{
			case 0:
				w = 340;
				h = 30;
				break;
			case 1:
				w = 350;
				h = 80;
				break;

			case 2:
				w = 350;
				h = 320;
		}

		w = 400;
		h = 275;

		var s = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="' + name + '" width="' + w + '" height="' + h + '" ' +
                        'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> ' + 
                        '<param name="movie" value="' + WWL._libbase + '/uploader/gfx/uploader.swf" /> ' + 
                        '<param name="quality" value="high" /> ' + 
                        '<param name="bgcolor" value="#ffffff" /> ' + 
                        '<param name="allowNetworking" value="all" /> ' + 
                        '<param name="allowScriptAccess" value="always" /> ' +
			'<param name="flashVars" value="upl_id=' + orig_name + '&" />' + 
                        '<embed src="' + WWL._libbase + '/uploader/gfx/uploader.swf" quality="high" bgcolor="#ffffff" width="' + w + '" height="' + h + '" name="' + name + '" align="middle" ' +
                        'play="true" loop="false" quality="high" flashVars="upl_id=' + orig_name + '" allowNetworking="all" ' + 
			' allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"> ' +
                        '</embed> </object>';

		return s;
	};
	// }}}

	uploader.to_string = function ()
	{
		if ( uploader._is_html5 )
			return uploader._make_html5 ();
		else
			return uploader._make_flash ();
	};

	return uploader;
};

WWL.uploader.event = function ( upl_id, mode )
{
	var upl = WWL.get_instance ( "uploader", upl_id );

	if ( upl.cbacks [ mode ] ) upl.cbacks [ mode ] ();
};

// handlers for HTML5 type
WWL.uploader.change = function ( id, input )
{
	console.debug ( arguments );
	var inst = WWL.get_instance ( 'uploader', id );
	for ( var i = 0; i < input.files.length; i++ )
		inst._files.push ( input.files.item ( i ) );
	console.debug ( inst._files );
	inst._clear_input ();
	inst._render_files ();
};

WWL.uploader.remove_file = function ( id, index )
{
	var inst = WWL.get_instance ( 'uploader', id );
	inst._files.del ( index );
	inst._files = inst._files.values ();
	inst._render_files ();
};

WWL.uploader.TYPE_MINI  = 0;
WWL.uploader.TYPE_SMALL = 1;
WWL.uploader.TYPE_FULL  = 2;
